var testAccount  = "test"    + Date.now();
var testUserName = "测试用户" + Date.now(); 

module.exports = function(lab){
    return {

        "测试用户注册" : function(){

            return lab.backend.users.@register({

                account  : testAccount,
                name     : testUserName,
                password : "123456",

            }).then(function(createdOperator){

                lab.backend.user.@getByAccount(testAccount).then(function(fetchOperator){
                    if (createdOperator.id == fetchOperator.id){
                        this.next(createdOperator);
                    } else {
                        this.reject("operator not found");
                    }
                }).pipe(this);

            })

        },

        "测试用户注册with组织" : function(){
            var session = {};
            return lab.backend.users.@register({
                account  : "ou" + testAccount,
                name     : "ou" + testUserName,
                password : "123456",
                ou_name  : "测试组织"  + Date.now()
            }).then(function(result){
                @dump(result);
                lab.backend.user.participant(session).pipe(this);

            }).then(function(){

                lab.backend.user.login(session, {
                    account : "ou" + testAccount,
                    password : "123456"
                }).pipe(this);

            }).then(function(result){

                @dump(result);
                @dump(session);

                lab.backend.ou.saveOU(session, {
                    services : {
                        "test"  : "http://localhost:12333"
                    }
                }).pipe(this);

            }).then(function(){
                lab.backend.user.newAccessKey(session).pipe(this);
            }).then(function(value){

                this.pool.key_session_value = value;
                lab.backend.keys.participant({},value).pipe(this);

            }).then(function(){
                lab.backend.keys.participant({},this.pool.key_session_value).finished(this.next);
            })
        },

        "用户登录" : function(){
            var session = {};
            return lab.backend.user.participant(session).then(function(result){
                
                @dump(result);

                lab.backend.user.login(session, {
                    account : testAccount,
                    password : "123456"
                }).pipe(this);

            });
        },

        "记住登陆" : function(){
            var session = {};
            return lab.backend.user.participant(session).then(function(participant){
                
                @dump(participant);

                lab.backend.user.login(session, {
                    account : testAccount,
                    password : "123456"
                }).resolve(participant).pipe(this);

            }).then(function(participant){

                @dump(session);
                @dump(participant);

                var new_session = {};
                lab.backend.user.participant(new_session,participant).resolve(new_session).pipe(this);

            });
        },

        "用户列表" : function() {
            
            return @.async(function(){
                lab.backend.users.list({
                    currentOU : {
                        id : @.uuid()
                    }
                },{
                    start : 0,
                    limit : 10
                }).pipe(this);
            })
        }
    }
}